package other;

public class LeetCode_415 {
    public static void main(String[] args) {
        LeetCode_415 leet = new LeetCode_415();
        String s = leet.addStrings("123", "4");
        System.out.println(s);
    }
    public String addStrings(String num1, String num2) {
        int[] nums = new int[10];
        for (int i = 0; i < 10; i++) {
            nums[i] = i;
        }
        if(num1.length() < num2.length()){
            String swap = num1;
            num1 = num2;
            num2 = swap;
        }
        int[] result = new int[num1.length() + 1];
        int rPoint = 0;
        int oPoint = num1.length() - 1;
        int tPoint = num2.length() - 1;
        int jinwei = 0;
        while (oPoint >= 0){
            int oNum = nums[num1.charAt(oPoint--) - 48];
            int tNum = 0;
            if(tPoint >= 0){
                tNum = nums[num2.charAt(tPoint--) - 48];
            }
            int h = oNum + tNum;
            if(jinwei > 0){
                h += jinwei;
                jinwei = 0;
            }
            int s = h / 10;
            int g = h % 10;
            if(s != 0){
                jinwei = s;
            }
            result[rPoint++] = g;
        }
        if(jinwei > 0){
            result[rPoint] = jinwei;
        }else {
            rPoint--;
        }
        StringBuilder re = new StringBuilder(100);
        for (int i = rPoint; i >= 0; i--) {
            re.append(result[i]);
        }
        return re.toString();
    }
}
